
var utils = require('../../utils/util.js');
var app = getApp()
Page({
  data: {
    // 页面配置  
    winWidth: 0,
    winHeight: 0,
    // tab切换 
    currentTab: 0,
    goodsList:[]
  },
  onLoad: function (options) {
    var currentTab = options.currentTab
    if (!currentTab) {
      currentTab = 0
    }
    this.setData({
      currentTab: currentTab
    })
  },
  onShow: function () {
    var _this = this
    app.getUserInfo(function (cb) {
      _this.getOrderList('all');
    })
  },
  onReady: function () {
    var that = this;
    // 获取系统信息 
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  // 滑动切换tab 
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  /**
   * 获取订单列表
   */
  getOrderList: function (state) {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Order&a=getOrderList",
      data: {
        state: state
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          _this.setData({
            goodsList: res.data.list
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  tapCancel:function (e) {
    var _this = this
    var orderNo = e.target.dataset.id
    wx.showModal({
      content: '确认取消该订单？',
      showCancel: true,
      cancelText: '不',
      confirmText: '确认',
      success: function(res) {
        if (res.confirm) {
          _this.updateOrderState(orderNo, 5)
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  tapPay: function (e) {
    var _this = this
    var orderNo = e.target.dataset.id
    wx.request({
      url: app.globalData.requestip + "&c=Order&a=rePay",
      data: {
        order_no: orderNo,
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          var data = JSON.parse(res.data.jsApiParameters)
          var pkg = 'prepay_id=' + res.data.prepay_id;
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success: function (res) {
              console.log(res)
              if (res.errMsg == 'requestPayment:ok') {
                wx.showToast({
                  title: '支付成功',
                  duration: 2000,
                  mask: true
                })
              }
            },
            fail: function (res) {
              console.log(res)
              if (res.errMsg == 'requestPayment:fail cancel') {
                wx.showToast({
                  title: '支付取消',
                  duration: 2000,
                  mask: true
                })

              }
            }
          })
        } else {
          utils.loadFail(res)
        }
      }
    });
  },

  tapFinish:function (e) {
    var _this = this
    var orderNo = e.target.dataset.id
    wx.showModal({
      content: '确认收货',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确认',
      success: function (res) {
        if (res.confirm) {
          _this.updateOrderState(orderNo, 4)
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  updateOrderState:function (orderNo,state) {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Order&a=updateOrderState",
      data: {
        order_no: orderNo,
        state: state
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          _this.getOrderList('all')
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  }
})